<?php
/***
 * 图片控制器
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Api;

use App\Services\ImgServices\ImgApiService;
use App\Services\ImgServices\ImgTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Response;
class ImgController extends Controller
{
    protected $imgApiSer;
    protected $imgTool;

    /**
     * ImgController constructor.
     */
    public function __construct(ImgApiService $imgApiSer,ImgTool $imgTool)
    {
        $this->imgApiSer=$imgApiSer;
        $this->imgTool=$imgTool;
    }

    /***
     * 保存图片
     * @param Request $request
     * @return array
     */
    public function saveImg(Request $request)
    {
        $rule=array(
            'image'=>'required|image',
        );
        $validator=$this->imgApiSer->store($request,$rule);
        $img_file=$request->file('image');
        $width=intval($request->input('width'));
        $high=intval($request->input('high'));
        $height=intval($request->input('height'));
        if(!empty($height)){
            $high=$height;
        }
        $is_need_thumb=true;
        if(empty($width)||empty($high)||(!is_int($width))||(!is_int($high))){
            $is_need_thumb=false;
        }
        $data['img_file']=$img_file;
        if ($validator->fails())
        {
            if(empty($img_file)){
                $result= GeneralTool::createResult('', 'M01001');
            }elseif($validator->errors()->first('image')){
                $result=GeneralTool::createResult('', 'M01002');
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        $original_image=$this->imgApiSer->saveImgAndReturnUrl($data);
        if ($is_need_thumb){
            $img_path=ImgTool::getImgPathByImgUrl($original_image);
            $thumbnail_path=$this->imgTool->getImgThumbByImgPath($img_path,$width,$high);
            $thumbnail=ImgTool::getImgUrlByImgPath($thumbnail_path);
        }else{
            $thumbnail=$original_image;
        }
        if(empty($original_image)){
            $result=GeneralTool::createResult('', 'M01002');
        }else{
            $result=GeneralTool::createResult(array('original_image'=>$original_image,'thumbnail'=>$thumbnail), 'M00000');
        }
        return Response::json($result);
    }

    /***
     * MD5获取图片链接
     * @param Request $request
     * @return mixed
     */
    public function getImgUrlByMd5(Request $request)
    {
        $rule=array(
            'image_md5'=>'required|string|',
        );
        $validator=$this->imgApiSer->store($request,$rule);
        $img_md5=trim($request->input('image_md5'));
        $data['img_md5']=$img_md5;
        $width=intval($request->input('width'));
        $high=intval($request->input('high'));
        //是否生成缩略图
        $is_need_thumb=true;
        if(empty($width)||empty($high)||(!is_int($width))||(!is_int($high))){
            $is_need_thumb=false;
        }
        if(empty($img_md5)){
            $result=GeneralTool::createResult('', 'M01004');
            return Response::json($result);
        }
        if ($validator->fails())
        {
            if($validator->errors()->first('img_md5')){
                $result= GeneralTool::createResult('', 'M01005');
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        $img_url=$this->imgApiSer->getImgUrlByMd5($img_md5);
        if ($is_need_thumb&&!empty($img_url)){
            $img_path=ImgTool::getImgPathByImgUrl($img_url);
            $thumbnail_path=$this->imgTool->getImgThumbByImgPath($img_path,$width,$high);
            $thumbnail=ImgTool::getImgUrlByImgPath($thumbnail_path);
        }else{
            $thumbnail=$img_url;
        }
        $result=GeneralTool::createResult(array('original_image'=>$img_url,'thumbnail'=>$thumbnail), 'M00000');
        return Response::json($result);

    }

    /****
     * md5数组字符串获取查询存储在数据库中的图片链接数组
     * @param Request $request
     * @return array
     */
    public function getImgUrlByMd5ArrStr(Request $request)
    {
        $rule=array(
            'img_md5_arr_str'=>'required|string|',
        );
        $validator=$this->imgApiSer->store($request,$rule);
        $img_md5_arr_str=trim($request->input('img_md5_arr_str'));
        $data['img_md5_arr_str']=$img_md5_arr_str;
        if(empty($img_md5_arr_str)){
            return GeneralTool::createResult('', 'M01006');
        }
        if ($validator->fails())
        {
            if($validator->errors()->first('img_md5_arr_str')){
                $result= GeneralTool::createResult('', 'M01007');
            }
            if(isset($result)&&sizeof($result)>0){
                return Response::json($result);
            }
        }
        $result=$this->imgApiSer->getImgUrlByMd5ArrStr($img_md5_arr_str);
        $result=GeneralTool::createResult($result, 'M00000');
        return Response::json($result);
    }
}
